package jjn.round1;

import commons.ListNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Jjn
 * @since 2022/7/29 23:05
 */
public class LeetCode234_PalindromeLinkedList {

    public boolean isPalindrome(ListNode head) {
        // 遍历把节点放到数组
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }

        // 双指针对比元素
        int size = list.size();
        int start = 0;
        int end = size - 1;
        while (start <= end) {
            Integer startElement = list.get(start);
            Integer endElement = list.get(end);
            if (!Objects.equals(startElement, endElement)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}
